class Solution
{
public:
    vector<int> sortedSquares(vector<int> &nums)
    {
        int n = nums.size();
        vector<int> result(n);
        int front = 0;
        int back = n - 1;
        int pos = n - 1;
        while (front <= back)
        {
            if (abs(nums[front]) > abs(nums[back]))
            {
                result[pos] = nums[front] * nums[front];
                ++front;
            }
            else
            {
                result[pos] = nums[back] * nums[back];
                --back;
            }
            --pos;
        }
        return result;
    }
};